package pkg.filter;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.PreparedQuery;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.Query;
import java.util.ArrayList;
import java.util.List;
import pkg.dao.Filter;
import pkg.entity.Auction;
import pkg.entity.Teacher;

public class AuctionFilter extends Filter {

    private Teacher teacher;
    private Auction auction;

    @Override
    public List getElements(Objectify ofy) {
        Query<Auction> query = ofy.query(Auction.class);

        //        check();
      if (auction != null) {
            query.filter("id =", auction.getId());
        }
        if (teacher != null) {
            query.filter("teacherKey", new Key<Teacher>(Teacher.class, teacher.getId()));
        }
        List<Auction> entities = new ArrayList<Auction>();
        for (Auction e : query) {
            entities.add(e);
        }
        return entities;
    }

    @Override
    public int getCount(Objectify ofy) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void setSimple(String filterText) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    public Teacher getTeacher() {
        return teacher;
    }

    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }

    public void setAuction(Auction auction) {
        this.auction = auction;
    }

    private void check() {
        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        com.google.appengine.api.datastore.Query q = new com.google.appengine.api.datastore.Query("Auction");
//q.addFilter("lastName", Query.FilterOperator.EQUAL, lastNameParam);
//q.addFilter("height", Query.FilterOperator.LESS_THAN, maxHeightParam);

// PreparedQuery contains the methods for fetching query results
// from the datastore
        PreparedQuery pq = datastore.prepare(q);

        for (Entity result : pq.asIterable()) {
            System.out.println("result = " + result);
//            String firstName = (String) result.getProperty("firstName");
//            String lastName = (String) result.getProperty("lastName");
//            Long height = (Long) result.getProperty("height");
//            System.out.println(lastName + " " + firstName + ", " + height.toString() + " inches tall");
        }
    }
}
